package mamikeji.liaowa.ui.activity.circle;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.CircleTypeBean;
import mamikeji.liaowa.http_api.ForHttp;
import mamikeji.liaowa.http_api.HttpApi;
import mamikeji.liaowa.http_api.HttpKey;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.ui.adapter.CircleTypeGridAdapter;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.utils.ForToast;
import mamikeji.liaowa.utils.MultiStateView;

public class CircleTypeActivity extends BaseActivity {
    private static final String TAG = "CircleTypeActivity";
    @BindView(R.id.gridlayout_circle_type)
    GridView gridlayoutCircleType;
    @BindView(R.id.multiStateView_circle_type)
    MultiStateView multiStateViewCircleType;

    CircleTypeGridAdapter circleTypeGridAdapter;

    List<CircleTypeBean.DataEntity> dataEntityList = new ArrayList<>();//数据集

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_type);
        ButterKnife.bind(this);
        getBase();
        setTitleLeftBtn();
        setTitleName(R.string.circle_type_name);
        setTitleRightBtn(R.string.circle_type_ok, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String types = "";
                for (int i = 0; i < dataEntityList.size(); i++) {
                    if (dataEntityList.get(i).getIs_choice() == 1) {
                        types = types + dataEntityList.get(i).getType_id() + ",";
                    }
                }

                SengCircleType(types);
            }
        });

        circleTypeGridAdapter = new CircleTypeGridAdapter(dataEntityList, this);
        gridlayoutCircleType.setAdapter(circleTypeGridAdapter);

        getCircleType();

        gridlayoutCircleType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                dataEntityList.get(position).setIs_choice(dataEntityList.get(position).getIs_choice() == 1 ? 0 : 1);
                circleTypeGridAdapter.notifyDataSetChanged();
            }
        });
    }

    private void getCircleType() {
        Map<String, String> formMap = new HashMap<>();
        ForHttp.setMsg(
                HttpApi.ROOT_PATH + HttpApi.GetCircleType + "?usercode=" + ForPreference.getString(ForContents.USER_CODE)
                , formMap, new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        ForToast.showToast(R.string.intent_error);
                        multiStateViewCircleType.setViewState(MultiStateView.VIEW_STATE_NOINTERNET);
                    }

                    @Override
                    public void onResponse(String response) {
                        ForLog.i("CircleTypeActivity", response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String status = jsonObject.getString(HttpKey.STATUS);
                            if ("200".equals(status)) {
                                CircleTypeBean circleTypeBean = new Gson().fromJson(response, CircleTypeBean.class);

                                dataEntityList.clear();
                                dataEntityList.addAll(circleTypeBean.getData());

                                circleTypeGridAdapter.setList(dataEntityList);
                                multiStateViewCircleType.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                            } else {
                                multiStateViewCircleType.setViewState(MultiStateView.VIEW_STATE_ERROR);
                                ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                            }
                            hideLoading();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 提交分类
     */
    public void SengCircleType(String types) {
        showLoading();
        Map<String, String> formMap = new HashMap<>();
        formMap.put(HttpKey.Submit_circletype_usercode, ForPreference.getString(ForContents.USER_CODE));
        formMap.put(HttpKey.Submit_circletype_ids, types);
        ForHttp.setMsg(HttpApi.ROOT_PATH + HttpApi.SengCircleType, formMap, new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                ForLog.i(TAG, e.getMessage());
                ForToast.showToast(R.string.intent_error);
                hideLoading();
            }

            @Override
            public void onResponse(String response) {
                ForLog.i(TAG, response);
                hideLoading();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String status = jsonObject.getString(HttpKey.STATUS);
                    if ("200".equals(status)) {
                        setResult(RESULT_OK, new Intent());
                        finish();
                    } else {
                        ForToast.showToast(jsonObject.getString(HttpKey.MESSAGE));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
